<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java类型转换解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=NotoSans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero-bg {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .code-block {
            background: #2d2d2d;
            border-radius: 8px;
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
        }
        .type-badge {
            display: inline-block;
            padding: 0.25rem 0.5rem;
            border-radius: 4px;
            font-size: 0.75rem;
            font-weight: 600;
        }
        .divider {
            width: 100%;
            height: 1px;
            background: linear-gradient(90deg, transparent, rgba(0,0,0,0.1), transparent);
            margin: 2rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-bg text-white py-20 px-4">
        <div class="container mx-auto max-w-4xl">
            <div class="flex flex-col items-center text-center">
                <h1 class="text-4xl md:text-5xl font-bold mb-4 font-serif">Java类型转换解析</h1>
                <p class="text-xl md:text-2xl mb-8 max-w-2xl">深入理解基本数据类型转换与集合操作</p>
                <div class="flex space-x-4">
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">类型转换</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">集合操作</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full">Java基础</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-4xl px-4 py-12">
        <!-- Problem Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 font-serif text-gray-800 flex items-center">
                <i class="fas fa-question-circle mr-3 text-blue-500"></i>
                问题代码分析
            </h2>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-8">
                <div class="p-6">
                    <div class="code-block p-6 mb-6">
                        <code class="text-gray-300">
                            <span class="text-purple-300">Set</span>&lt;<span class="text-blue-300">Short</span>&gt; set = <span class="text-blue-300">new</span> <span class="text-purple-300">HashSet</span>&lt;&gt;();<br>
                            <span class="text-blue-300">for</span> (<span class="text-blue-300">short</span> i = <span class="text-yellow-300">0</span>; i &lt; <span class="text-yellow-300">5</span>; i++) {<br>
                            &nbsp;&nbsp;set.<span class="text-yellow-300">add</span>(i);<br>
                            &nbsp;&nbsp;set.<span class="text-yellow-300">remove</span>(i - <span class="text-yellow-300">1</span>);<br>
                            }<br>
                            <span class="text-purple-300">System</span>.out.<span class="text-yellow-300">println</span>(set.<span class="text-yellow-300">size</span>());
                        </code>
                    </div>
                    
                    <div class="prose max-w-none">
                        <p class="text-lg text-gray-700 mb-4">
                            程序执行结束后的的结果是<strong class="text-red-500">5</strong>，也就是说明没有删除任何元素。
                        </p>
                        <p class="text-lg text-gray-700 mb-4">
                            <i class="fas fa-lightbulb text-yellow-500 mr-2"></i>
                            <strong>原因分析：</strong> <code>i</code>是<code>short</code>类型，<code>1</code>是<code>int</code>类型，这两个计算完的结果会自动提升为<code>int</code>类型，而<code>set</code>中存储的是<code>Short</code>对象，所以无法删除。
                        </p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Solution Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 font-serif text-gray-800 flex items-center">
                <i class="fas fa-check-circle mr-3 text-green-500"></i>
                解决方案
            </h2>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-8">
                <div class="p-6">
                    <div class="code-block p-6 mb-6">
                        <code class="text-gray-300">
                            <span class="text-purple-300">Set</span>&lt;<span class="text-blue-300">Short</span>&gt; set = <span class="text-blue-300">new</span> <span class="text-purple-300">HashSet</span>&lt;&gt;(); <span class="text-gray-500">// 0 1 2 3 4</span><br>
                            <span class="text-blue-300">for</span> (<span class="text-blue-300">short</span> i = <span class="text-yellow-300">0</span>; i &lt; <span class="text-yellow-300">5</span>; i++) {<br>
                            &nbsp;&nbsp;set.<span class="text-yellow-300">add</span>(i);<br>
                            &nbsp;&nbsp;<span class="text-blue-300">short</span> x = (<span class="text-blue-300">short</span>) (i - <span class="text-yellow-300">1</span>); <span class="text-gray-500">// 把计算后的结果转short</span><br>
                            &nbsp;&nbsp;set.<span class="text-yellow-300">remove</span>(x);<br>
                            }<br>
                            <span class="text-purple-300">System</span>.out.<span class="text-yellow-300">println</span>(set.<span class="text-yellow-300">size</span>());
                        </code>
                    </div>
                    
                    <div class="prose max-w-none">
                        <p class="text-lg text-gray-700 mb-4">
                            <i class="fas fa-info-circle text-blue-500 mr-2"></i>
                            在修正后的代码中，我们显式地将<code>(i - 1)</code>的结果强制转换为<code>short</code>类型，这样就能正确地从集合中删除元素。
                        </p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Type Conversion Explanation -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 font-serif text-gray-800 flex items-center">
                <i class="fas fa-exchange-alt mr-3 text-purple-500"></i>
                Java类型转换机制
            </h2>
            
            <div class="grid md:grid-cols-2 gap-6">
                <!-- Automatic Type Promotion -->
                <div class="card bg-white rounded-xl shadow-md overflow-hidden hover:shadow-lg">
                    <div class="p-6">
                        <h3 class="text-xl font-bold mb-4 text-blue-600 flex items-center">
                            <i class="fas fa-random mr-2"></i>
                            自动类型提升
                        </h3>
                        <div class="mb-4">
                            <span class="type-badge bg-blue-100 text-blue-800 mr-2">byte</span>
                            <span class="type-badge bg-blue-100 text-blue-800 mr-2">short</span>
                            <span class="type-badge bg-blue-100 text-blue-800 mr-2">char</span>
                            <span class="type-badge bg-blue-100 text-blue-800">int</span>
                            <i class="fas fa-arrow-right mx-2 text-gray-400"></i>
                            <span class="type-badge bg-green-100 text-green-800">long</span>
                            <i class="fas fa-arrow-right mx-2 text-gray-400"></i>
                            <span class="type-badge bg-purple-100 text-purple-800">float</span>
                            <i class="fas fa-arrow-right mx-2 text-gray-400"></i>
                            <span class="type-badge bg-red-100 text-red-800">double</span>
                        </div>
                        <p class="text-gray-700">
                            在表达式中，如果操作数的类型不同，Java会自动将较小范围的数据类型提升为较大范围的数据类型。这就是为什么<code>short - int</code>会得到<code>int</code>结果。
                        </p>
                    </div>
                </div>
                
                <!-- Explicit Type Casting -->
                <div class="card bg-white rounded-xl shadow-md overflow-hidden hover:shadow-lg">
                    <div class="p-6">
                        <h3 class="text-xl font-bold mb-4 text-purple-600 flex items-center">
                            <i class="fas fa-hand-paper mr-2"></i>
                            强制类型转换
                        </h3>
                        <div class="mb-4">
                            <span class="type-badge bg-red-100 text-red-800 mr-2">double</span>
                            <i class="fas fa-arrow-right mx-2 text-gray-400"></i>
                            <span class="type-badge bg-purple-100 text-purple-800 mr-2">float</span>
                            <i class="fas fa-arrow-right mx-2 text-gray-400"></i>
                            <span class="type-badge bg-green-100 text-green-800 mr-2">long</span>
                            <i class="fas fa-arrow-right mx-2 text-gray-400"></i>
                            <span class="type-badge bg-blue-100 text-blue-800">int</span>
                        </div>
                        <p class="text-gray-700">
                            当我们需要将较大范围的数据类型转换为较小范围的数据类型时，必须使用强制类型转换语法<code>(type)</code>。这可能会导致精度丢失或数据截断，需要谨慎使用。
                        </p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Visualization Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 font-serif text-gray-800 flex items-center">
                <i class="fas fa-project-diagram mr-3 text-indigo-500"></i>
                类型转换过程可视化
            </h2>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden p-6">
                <div class="mermaid">
                    graph TD
                    A[short i = 0] --> B[i - 1]
                    C[int 1] --> B
                    B --> D[int result]
                    D -->|强制转换| E[short x]
                    E --> F[set.remove(x)]
                </div>
                <div class="mt-4 text-sm text-gray-500">
                    <i class="fas fa-info-circle mr-2"></i>
                    鼠标悬停在图表元素上可以查看详细信息
                </div>
            </div>
        </section>

        <!-- Key Takeaways -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-6 font-serif text-gray-800 flex items-center">
                <i class="fas fa-key mr-3 text-orange-500"></i>
                关键要点
            </h2>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="card bg-white rounded-xl shadow-md overflow-hidden hover:shadow-lg">
                    <div class="p-6">
                        <div class="w-12 h-12 bg-blue-100 rounded-full flex items-center justify-center mb-4">
                            <i class="fas fa-exclamation-triangle text-blue-500 text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-2">类型匹配</h3>
                        <p class="text-gray-700">
                            集合操作时要注意类型匹配，特别是使用自动装箱/拆箱时。
                        </p>
                    </div>
                </div>
                
                <div class="card bg-white rounded-xl shadow-md overflow-hidden hover:shadow-lg">
                    <div class="p-6">
                        <div class="w-12 h-12 bg-purple-100 rounded-full flex items-center justify-center mb-4">
                            <i class="fas fa-random text-purple-500 text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-2">类型提升</h3>
                        <p class="text-gray-700">
                            Java会在表达式中自动执行类型提升，可能导致意外的结果。
                        </p>
                    </div>
                </div>
                
                <div class="card bg-white rounded-xl shadow-md overflow-hidden hover:shadow-lg">
                    <div class="p-6">
                        <div class="w-12 h-12 bg-green-100 rounded-full flex items-center justify-center mb-4">
                            <i class="fas fa-check-circle text-green-500 text-xl"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-2">显式转换</h3>
                        <p class="text-gray-700">
                            当需要特定类型时，使用显式类型转换确保结果符合预期。
                        </p>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto max-w-4xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                    <p class="text-gray-400">分享编程知识与技术见解</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-blue-300 hover:text-blue-100 transition-colors duration-300">
                        <i class="fas fa-globe mr-2"></i>
                        http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>